/*
	Programação Concorrente
	Trabalho 2
	Turma A

	Adilson Abreu Couto Rapozo Junior ( n. USP: 6402336 )
	Luiz Alfredo Bertoni ( n. USP: 6418834 )
	Rodrigo Fernandes da Costa ( n. USP: 6793073 )
*/

/*
	Labels

	IN: indicates that a pointer is going to be used to retrieve data.
	OUT: indicates that a pointer is going to be used to store data.
	INOUT: indicates that a pointer is going to be used to store and retrieve data.
*/

/*
	Remarks

	A string is an array of characters that have the last character equal to \0.
	Character make reference to the type "char" and have 1 byte on the systems used to run the program.
*/

#ifndef _MAIN_H_

#define _MAIN_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "mpi.h"

#define FILE_NAME_SIZE 100

/* Palindrome functions */

/*
	Name: isPalindrome
	Description: This function can be used either to find if a word is a palindrome or a phrase.
	Arguments:
		pWord(IN): must be a pointer to a string.
	Return: 1 if the word is a palindrome and 0 otherwise.
*/
int isPalindrome ( char * pWord ) ;


/* Prime number functions*/

/*
	Name: isPrime
	Description: This function is used to calculate the sum of the ASCII values of the palindrome.
	Arguments:
		pWord(IN): must be a pointer to a string.
	Return: 1 if the sum is a prime number and 0 otherwise.
*/
int isPrime ( char * pWord );


/* File functions */

/*
	Name: getWords
	Description: This function get the words from a file, after the file is copied to memory.
	Arguments:
		pFileBuffer(IN): The file data in memory, an array of characters.
		fileBufferSize: The number of characters in pFileBuffer.
		ppWordBuffer(OUT): A pointer to the buffer with the words.
		pWordNumber(OUT): A pointer to the number of the words.
		pWordSize(OUT): A pointer to the max number of characters in a word inclusing \0.
*/
void getWords ( char * pFileBuffer , int fileBufferSize , char ** ppWordBuffer , int * pWordNumber , int * pWordSize ) ;

/*
	Name: getPhrases
	Description: This function get the phrases from a file, after the file is copied to memory.
	Arguments:
		pFileBuffer(IN): The file data in memory, an array of characters.
		fileBufferSize: The number of characters in pFileBuffer.
		ppPhraseBuffer(OUT): A pointer to the buffer with the phrases.
		pPhraseNumber(OUT): A pointer to the number of the phrases.
		pPhraseSize(OUT): A pointer to the max number of characters in a phrase including \0.
*/
void getPhrases ( char * pFileBuffer , int fileBufferSize , char ** ppPhraseBuffer , int * pPhraseNumber , int * pPhraseSize ) ;

/*
	Name: isPhraseSeparator
	Description: This function tells if a character marks the end of a phrase.
	Arguments:
		character: A character.
	Return: 1 if the character marks the end of a phrase and 0 otherwise.
*/
int isPhraseSeparator ( char character ) ;

/*
	Name: readFile
	Description: This function copies the file to memory and separates words and phrases.
	Arguments:
		pFileName(IN): A string with the name of the file to copy.
		ppFileBuffer(OUT): The pointer to the file data in memory, a pointer to an array of characters.
		pFileBufferSize(OUT): A pointer to the number of characters in the array pointed to ppFileBuffer.
*/
void readFile ( char * pFileName , char ** ppFileBuffer , int * pFileBufferSize ) ;

/* Process functions */

/*
	Name: firstProcess
	Description: This function is executed in the "master" node.
	Arguments:
		numberOfProcess: Number of process.
		argc: argc from main.
		argv: argv from main.
*/
void firstProcess ( int numberOfProcess , int argc , char ** argv ) ;

/*
	Name: regularProcess
	Description: This function is executed in the other nodes.
*/
void regularProcess ( void ) ;

/*
	Name: findWordPalindromes
	Description:
		This function divides the words between the nodes.
		Process its share of words.
		Gather the results from all the nodes.
		Send the result to the screen.
	Arguments:
		numberOfProcess: Number of process.
		pFileBuffer(IN): An array with the file content in memory.
		fileBufferSize: The number of elements in pFileBuffer.
		prime: 1 if the program must check for prime number and 0 otherwise.
*/
void findWordPalindromes ( int numberOfProcess , char * pFileBuffer , int fileBufferSize , int prime ) ;

/*
	Name: helpFindWordPalindromes
	Description: This function Process its share of words and send the result to the "master" node.
*/
void helpFindWordPalindromes ( int prime ) ;

/*
	Name: findPhrasePalindromes
	Description:
		This function divides the phrases between the nodes.
		Process its share of phrases.
		Gather the results from all the nodes.
		Send the result to the screen.
	Arguments:
		numberOfProcess: Number of process.
		pFileBuffer(IN): An array with the file content in memory.
		fileBufferSize: The number of elements in pFileBuffer.
*/
void findPhrasePalindromes ( int numberOfProcess , char * pFileBuffer , int fileBufferSize ) ;

/*
	Name: helpFindPhrasePalindromes
	Description: This function Process its share of phrases and send the result to the "master" node.
*/
void helpFindPhrasePalindromes ( void ) ;

/* Command line arguments functions. */

/*
	Name: readArguments
	Description: Process the arguments from the command line.
	Arguments:
		argc: argc from main.
		argv: argv from main.
		pWord: Pointer to a flag to check for palindrome in words.
		pPhrase: Pointer to a flag to check for palindrome in phrases.
		pPrime: Pointer to a flag to check for prime numbers.
		pFileName: Pointer to an array of characters that have a string with the name of the file to get words or phrases.
*/
int readArguments ( int argc , char ** argv , int * pWord , int * pPhrase , int * pPrime , char * pFileName ) ;

/*
	Name: showHelp
	Description: Show program usage.
*/
void showHelp ( void ) ;

#endif

